﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Net;
using System.ServiceModel.Channels;

namespace S2S.MobileAPIService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MobileAPIService" in code, svc and config file together.
    public class MobileAPIService : IMobileAPIService
    {
        public void DoWork()
        {

        }
    }

    public class WebHttpBehaviorFaultErrorHandler : WebHttpBehavior
    {

        protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new FaultErrorHandler());
        }

    }

    public class FaultErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
            WebOperationContext.Current.OutgoingResponse.StatusDescription = error.Message;
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml; charset=utf-8";

            fault = Message.CreateMessage(version, FaultCode.CreateSenderFaultCode(error.Source,"http://www.thatindigogirl.com/samples/2008/01"), error.Message, null);
        }
    }
}
